#ifndef _PROTOCOL_XML_H_
#define _PROTOCOL_XML_H_

#include "byte.h"

enum
{
    XML_SIGN_ROOT = 0,
    XML_SIGN_NODE,
    XML_SIGN_ATTR
};

class CXmlBuffer : public CByteBuffer
{
    CXmlBuffer(const CXmlBuffer &r);

    CXmlBuffer& operator= (const CXmlBuffer& r);

public:

    CXmlBuffer(const char* name = "");

    CXmlBuffer(const char* inbuf, ushort inlen, const char* name = "");

    virtual ~CXmlBuffer(){}

    virtual int ToBlock(CByteBlock* block);

private:

    bool FindLabel(const char* &begin, ushort len, const char* &label, ushort &labelsize);

    bool ParseLabel(CByteBlock* block, const char* label, ushort labelsize);
};

class CXmlBlock : public CByteBlock
{
    template<typename T> friend CByteBlock* AddChild(CByteBlock* block, uchar sign);

    CXmlBlock(const CXmlBlock &r);

    CXmlBlock& operator= (const CXmlBlock& r);

    CXmlBlock(uchar sign, CByteBlock* parent);

public:

    CXmlBlock(const char* blockname = "");

    virtual ~CXmlBlock(){}

    bool SetXmlHeader();

    virtual int ToBuffer(CByteBuffer* buffer);
    
private:

    string  m_xmlheader;
};

bool en_xml_node(CByteBlock& block, string name, string value);

bool en_xml_attr(CByteBlock& block, string name, string value);

bool de_xml_node(CByteBlock& root, string name, string& value, uchar index = 1);

bool de_xml_attr(CByteBlock& root, string name, string& value, uchar index = 1);

#endif //_PROTOCOL_XML_H_
